<?php
namespace app\admin\controller;
use think\Db;
class Paymethod extends Base
{
    //列表
    public function lists()
    {
        if(is_post()){
            $cur_page = input('page');
            $keyword  = input('keyword');
            $status  = input('status');
            $type  = input('type');
            $min_time = input('min_time');
            $max_time = input('max_time');
            $sql = "
                select
                    [1]
                from
                    pay_method
                ";
            $param = [];
            $w = ['where'];
            if(!empty($min_time)){
                $w[] = ' and create_time>?';
                $param[] = strtotime($min_time);
            }
            if(!empty($max_time)){
                $w[] = ' and create_time<?';
                $param[] = strtotime($max_time);
            }
            if(!empty($keyword)){
                $w[] = ' and (title like ? or uuid like ?)';
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if($status!='100all'){
                $w[] = ' and status = ?';
                $param[] = $status;
            }
            if($type!='100all'){
                $w[] = ' and type = ?';
                $param[] = $type;
            }
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "*";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by sort desc, id desc limit {$start},{$page_size}";
            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['create_time'] = com_formate_time($v['create_time']);
                $rslist[$k]['checkbox'] = com_checkbox($v['id']);
                $rslist[$k]['sort']   = com_sort('pay_method',$v['id'],$v['sort']);
                $rslist[$k]['status'] = c('type.status',$v['status']);
                $rslist[$k]['type'] = c('type.pay_method',$v['type']);
                $rslist[$k]['action']  = com_btn_edit("/paymethod/edit/id/{$v['id']}");
                $rslist[$k]['action']  .= com_btn_del("/paymethod/delete/id/{$v['id']}", "您确定要删除【{$v['title']}】？");
            }
            $body_html = com_list_body($rslist, ["checkbox","title","show_title","type","uuid","ext","sort", "status", "create_time", "action"]);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        return view('',['status'=>c('type.status'),'type' => c('type.pay_method')]);
    }
    #添加
    function add(){
      if(is_post()){
            $d = post();
            $rule = [
                'title'=>  'require',
                'show_title'=>  'require',
                'uuid' =>  'require|unique:pay_method',
                'config' =>  'require',
            ];
            $msg = [
              'title.require'   =>  '请输入标题',
              'show_title.require'   =>  '请输入显示名称',
              'uuid.require'   =>  '请输入标识',
              'uuid.unique'   =>  '已存在的标识',
              'config.require'   =>  '请输入配置',
            ];
            com_validate($rule, $msg, $d);
            $d['create_time'] = time();
            $d['update_time'] = time();
            Db::name("pay_method")->insert($d);
            com_check(['suc_msg'=>'添加成功!','suc_js'=>'alert|close|reload']);
      }
      $d = [
        'type' => c('type.pay_method'),
        'status' => c('type.status'),
      ];
      return view('',$d);
    }
    #编辑
    function edit($id){
      if(is_post()){
            $d = post();
            $rule = [
                'title'=>  'require',
                'show_title'=>  'require',
                'uuid' =>  "require|unique:pay_method,uuid,{$id},id",
                'config' =>  'require',
            ];
            $msg = [
              'title.require'   =>  '请输入标题',
              'show_title.require'   =>  '请输入显示名称',
              'uuid.require'   =>  '请输入标识',
              'uuid.unique'   =>  '已存在的标识',
              'config.require'   =>  '请输入配置',
            ];
            com_validate($rule, $msg, $d);
            $d['update_time'] = time();
            Db::name("pay_method")->update($d);
            com_check(['suc_msg'=>'修改成功!','suc_js'=>'alert|close|reload']);
      }
      $info = Db::name('pay_method')->find($id);
      $d = [
        'type' => c('type.pay_method'),
        'status' => c('type.status'),
        'info' => $info,
      ];
      return view('',$d);
    }
    #删除
    function delete($id=''){
        Db::name('pay_method')->where('id','in',[$id])->delete();
        com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
    }

    #批量删除
    function delall(){
      if(is_post()){
          $ids = input('ids/a');
          Db::name('pay_method')->where('id','in',$ids)->delete();
          com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
      }
    }
}
